#ifndef EVM_PARSER_H
#define EVM_PARSER_H

#include "http_parser.h"
#include "request.h"
#include "common/file.h"
#include "util.h"

#include "common/log.h"


namespace evm {
    namespace http {

        using namespace std;

        struct Url {
            // string schema;
            // string host;
            // uint16_t port;
            string path;
            QueryParas query_paras;
            // string fragment;
            // string userinfo;

            Url(const string& url);
        };

        class HTTPConnection;

        struct Parser: public http_parser {
            Parser(HTTPConnection* handler);

            bool check_version(int major, int minor) const;

            const string& get_header(const string& field) const;

            bool has_header(const string& field, const string& value) const;

            Request& get_request();

            void clear();

            size_t execute(const char* buffer, size_t length);

            void end();

            static int on_message_begin(http_parser* hp);

            static int on_url(http_parser* hp, const char* at, size_t length);

            static int on_header_field(http_parser* hp, const char* at, size_t length);

            static int on_header_value(http_parser* hp, const char* at, size_t length);

            static int on_headers_complete(http_parser* hp);

            static int on_body(http_parser* hp, const char* at, size_t length);

            static int on_message_complete(http_parser* hp);

            static int on_chunk_header(http_parser* hp);

            static int on_chunk_complete(http_parser* hp);

            size_t total_parsed{};

        private:
            HTTPConnection* handler_;

            Request& request_;

            int header_building_state_ = 0;
            string header_field_;
            string header_value_;
        };

    }
}

#endif //EVM_PARSER_H
